package com.ruoyi.mq.controller;

//import cn.hutool.core.date.DateUtil;
//import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.mq.bean.SysConstant;
import com.ruoyi.mq.service.serviceImpl.MsgServiceImpl;
//import com.ruoyi.system.domain.GoodsCategory;
//import com.ruoyi.system.domain.Router;
//import com.ruoyi.system.service.IGoodsCategoryService;
//import com.ruoyi.system.service.IRouterService;
import com.ruoyi.system.service.IGwRouterService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author japhet_jiu
 * @version 1.0
 * MQ消息生产者
 */

@RestController
@RequestMapping("rabbitmq")
@Slf4j
public class MQController {

    @Value("${node.prefix}")
    private String routeKey;

    @Value("${topic.destination}")
    private String destination;
    @Autowired
    private MsgServiceImpl msgService;

//    @Autowired
//    private IGoodsCategoryService goodsCategoryService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IGwRouterService gwRouterService;


    @Autowired
    private AmqpTemplate amqpTemplate;
    /**
     *发码量生产者
     */
    @GetMapping("sendCodeMsg")
    public String sendCodeMsg() throws JsonProcessingException {
        int codeNum = 1;
        Map<String,Object> mapMsg = new HashMap<String,Object>();
        mapMsg.put("strategy", SysConstant.CODE);
        mapMsg.put("data",codeNum);

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonMessage = objectMapper.writeValueAsString(mapMsg);
        //发送消息
       // msgService.sendToManyConsumerByRoute(SysConstant.NIOT_TOPIC_EXCHANGE,"#",mapMsg);
       // amqpTemplate.convertAndSend(SysConstant.NIOT_FANOUT_EXCHANGE, routeKey, "mapMsg");
        amqpTemplate.convertAndSend(SysConstant.FANOUT_EXCHANGE, "", jsonMessage);
        return "发送消息成功"+new Date();
    }

    /**
     *解析量生产者
     */
    @GetMapping("sendParseMsg")
    public String sendParseMsg() {
        Map<String,Object> mapMsg = new HashMap<String,Object>();
        mapMsg.put("strategy", SysConstant.PARSE);
        mapMsg.put("data",1);
        //发送消息
        msgService.sendToManyConsumerByRoute(SysConstant.NIOT_TOPIC_EXCHANGE,routeKey,mapMsg);
        return "发送消息成功"+new Date();
    }

    /**
     *公司信息生产者
     */
    @GetMapping("sendCompanyMsg")
    public String sendCompanyMsg(Long id) {
        Map<String,Object> mapMsg = new HashMap<String,Object>();
        mapMsg.put("strategy", SysConstant.COMPANY);
        mapMsg.put("data",sysDeptService.selectDeptById(id));
        //发送消息
        msgService.sendToManyConsumerByRoute(SysConstant.NIOT_TOPIC_EXCHANGE,routeKey,mapMsg);
        return "发送消息成功"+new Date();
    }

    /**
     *路由信息生产者
     */
    @GetMapping("sendRouteMsg")
    public String sendRouteMsg(Long id) {
        Map<String,Object> mapMsg = new HashMap<String,Object>();
        mapMsg.put("strategy", SysConstant.COMPANY);
        mapMsg.put("data",gwRouterService.selectGwRouterById(id));
        //发送消息
        msgService.sendToManyConsumerByRoute(SysConstant.NIOT_TOPIC_EXCHANGE,routeKey,mapMsg);
        return "发送消息成功"+new Date();
    }
}
